package com.dongan.service.impl;
import com.dongan.pojo.ConfigFileKind;
import com.dongan.mapper.ConfigFileKindMapper;
import com.dongan.service.ConfigFileKindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 配置文件类型 服务实现类
 * </p>
 *
 * @author longTao
 * @since 2021-06-15
 */
@Service
public class ConfigFileKindServiceImpl extends ServiceImpl<ConfigFileKindMapper, ConfigFileKind> implements ConfigFileKindService {
    @Autowired
    private ConfigFileKindMapper mapper;
    @Override
    public List<ConfigFileKind> queryFileKind() {
        List<ConfigFileKind> list = querySecondKind(mapper.selectList(null));
        return list;
    }

    /**
     * 查找二级种类
     * @param list
     * @return
     */
    private List<ConfigFileKind> querySecondKind(List<ConfigFileKind> list){
        List<ConfigFileKind> list1=new ArrayList<>();
        for (ConfigFileKind kind : list) {
            if (Integer.valueOf(kind.getPId().toString())==0){
                list1.add(queryChildKind(kind,list));
            }
        }
        return list1;
    }

    /**
     * 查找它的子种类
     * @param configFileKind
     * @param list
     * @return
     */
    private ConfigFileKind queryChildKind(ConfigFileKind configFileKind,List<ConfigFileKind> list){
        for (ConfigFileKind kind : list) {
            if (configFileKind.getId()==Long.valueOf(kind.getPId().toString())){
                if (configFileKind.getList()==null){
                    configFileKind.setList(new ArrayList<>());
                }
                configFileKind.getList().add(queryChildKind(kind,list));
            }
        }
        return configFileKind;
    }
}
